package com.yanqu.road.dao.impl.tradewar;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.tradewar.UserTradeWarRecord;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;

public class CrossTradeWarRecordDaoImpl extends TempDataObjectDao<UserTradeWarRecord> {

	public LinkedList<UserTradeWarRecord> getUserTradeWarRecordList(long userId, int activityId, int maxCount) {
		ResultSet rs = null;
		CallableStatement call = null;
		LinkedList<UserTradeWarRecord> dataList = new LinkedList<>();
		String sql = "select * from t_u_cross_trade_war_record where userId=? and activityId=? order by recordTime desc limit ?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			call.setInt(2, activityId);
			call.setInt(3, maxCount);
			rs = call.executeQuery();
			while (rs.next()) {
				UserTradeWarRecord data = parse(rs);
				dataList.addFirst(data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataList;
	}

	public LinkedList<UserTradeWarRecord> getUserTradeWarRecordList(long userId, int activityId, long selectTime) {
		ResultSet rs = null;
		CallableStatement call = null;
		LinkedList<UserTradeWarRecord> dataList = new LinkedList<>();
		String sql = "select * from t_u_cross_trade_war_record where userId=? and activityId=? and recordTime>=? order by recordTime desc";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			call.setInt(2, activityId);
			call.setLong(3, selectTime);
			rs = call.executeQuery();
			while (rs.next()) {
				UserTradeWarRecord data = parse(rs);
				dataList.addFirst(data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataList;
	}

	@Override
	protected boolean addData(UserTradeWarRecord data) {
		PreparedStatement call = null;
		String sql = "INSERT INTO t_u_cross_trade_war_record(activityId,userId,recordUid,battleType,enemyUserId,enemyBaseData,enemyScore,killPatronsCount,"
				+ "loseScore,recordTime,isBattle) VALUES(?,?,?,?,?,?,?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setInt(i++, data.getActivityId());
			call.setLong(i++, data.getUserId());
			call.setString(i++, data.getRecordUid());
			call.setInt(i++, data.getBattleType());
			call.setLong(i++, data.getEnemyUserId());
			call.setString(i++, data.getEnemyBaseDataJsonStr());
			call.setLong(i++, data.getEnemyScore());
			call.setInt(i++, data.getKillPatronsCount());
			call.setInt(i++, data.getLoseScore());
			call.setLong(i++, data.getRecordTime());
			call.setBoolean(i++, data.isBattle());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected boolean updateData(UserTradeWarRecord data) {
		PreparedStatement call = null;
		String sql = "UPDATE t_u_cross_trade_war_record SET activityId=?,userId=?,battleType=?,enemyUserId=?,enemyBaseData=?,enemyScore=?,killPatronsCount=?,loseScore=?,"
				+ "recordTime=?,isBattle=? WHERE recordUid=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setInt(i++, data.getActivityId());
			call.setLong(i++, data.getUserId());
			call.setInt(i++, data.getBattleType());
			call.setLong(i++, data.getEnemyUserId());
			call.setString(i++, data.getEnemyBaseDataJsonStr());
			call.setLong(i++, data.getEnemyScore());
			call.setInt(i++, data.getKillPatronsCount());
			call.setInt(i++, data.getLoseScore());
			call.setLong(i++, data.getRecordTime());
			call.setBoolean(i++, data.isBattle());
			call.setString(i++, data.getRecordUid());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected UserTradeWarRecord parseData(ResultSet rs) throws SQLException {
		UserTradeWarRecord data = new UserTradeWarRecord();
		data.setActivityId(rs.getInt("activityId"));
		data.setUserId(rs.getLong("userId"));
		data.setRecordUid(rs.getString("recordUid"));
		data.setBattleType(rs.getInt("battleType"));
		data.setEnemyUserId(rs.getLong("enemyUserId"));
		String baseUserInfoStr = rs.getString("enemyBaseData");
		if(null == baseUserInfoStr){
			baseUserInfoStr = "";
		}
		UserBaseInfo userBaseInfo = JSON.parseObject(baseUserInfoStr, new TypeReference<UserBaseInfo>() {});
		if(null != userBaseInfo) {
			data.setEnemyBaseData(userBaseInfo);
		}
		data.setEnemyScore(rs.getLong("enemyScore"));
		data.setKillPatronsCount(rs.getInt("killPatronsCount"));
		data.setLoseScore(rs.getInt("loseScore"));
		data.setRecordTime(rs.getLong("recordTime"));
		data.setBattle(rs.getBoolean("isBattle"));
		return data;
	}
}
